Read target config later
authorAleksey Kladov <aleksey.kladov@gmail.com>
Sun, 24 Jul 2016 07:29:10 +0000 (10:29 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Sun, 24 Jul 2016 07:52:42 +0000 (10:52 +0300)
src/cargo/core/workspace.rs
src/cargo/ops/cargo_install.rs
src/cargo/ops/cargo_package.rs
src/cargo/util/config.rs

index f4a07b9234b6ecd7a7b99260de556d73da46131d..d4c32e57247667248a383c0c5707cb377f1e8b2c 100644 (file)
@@ -32,7 +32,8 @@ pub struct Workspace<'cfg> {
     // `current_manifest` was found on the filesystem with `[workspace]`.
     root_manifest: Option<PathBuf>,
 
-    // Allows to override the target directory for the purposes of `cargo install`.
+    // Shared target directory for all the packages of this workspace.
+    // `None` if the default path of `root/target` should be used.
     target_dir: Option<Filesystem>,
 
     // List of members in this workspace with a listing of all their manifest
@@ -81,6 +82,8 @@ impl<'cfg> Workspace<'cfg> {
     /// before returning it, so `Ok` is only returned for valid workspaces.
     pub fn new(manifest_path: &Path, config: &'cfg Config)
                -> CargoResult<Workspace<'cfg>> {
+        let target_dir = try!(config.target_dir());
+
         let mut ws = Workspace {
             config: config,
             current_manifest: manifest_path.to_path_buf(),
@@ -89,7 +92,7 @@ impl<'cfg> Workspace<'cfg> {
                 packages: HashMap::new(),
             },
             root_manifest: None,
-            target_dir: None,
+            target_dir: target_dir,
             members: Vec::new(),
         };
         ws.root_manifest = try!(ws.find_root(manifest_path));
@@ -107,7 +110,8 @@ impl<'cfg> Workspace<'cfg> {
     ///
     /// This is currently only used in niche situations like `cargo install` or
     /// `cargo package`.
-    pub fn one(package: Package, config: &'cfg Config, target_dir: Option<Filesystem>) -> Workspace<'cfg> {
+    pub fn one(package: Package, config: &'cfg Config, target_dir: Option<Filesystem>)
+               -> CargoResult<Workspace<'cfg>> {
         let mut ws = Workspace {
             config: config,
             current_manifest: package.manifest_path().to_path_buf(),
@@ -116,16 +120,21 @@ impl<'cfg> Workspace<'cfg> {
                 packages: HashMap::new(),
             },
             root_manifest: None,
-            target_dir: target_dir,
+            target_dir: None,
             members: Vec::new(),
         };
         {
             let key = ws.current_manifest.parent().unwrap();
             let package = MaybePackage::Package(package);
             ws.packages.packages.insert(key.to_path_buf(), package);
+            ws.target_dir = if let Some(dir) = target_dir {
+                Some(dir)
+            } else {
+                try!(ws.config.target_dir())
+            };
             ws.members.push(ws.current_manifest.clone());
         }
-        return ws
+        return Ok(ws)
     }
 
     /// Returns the current package of this workspace.
@@ -161,13 +170,9 @@ impl<'cfg> Workspace<'cfg> {
     }
 
     pub fn target_dir(&self) -> Filesystem {
-        if let Some(ref fs) = self.target_dir {
-            return fs.clone()
-        }
-        if let Some(fs) = self.config.target_dir() {
-            return fs.clone()
-        }
-        Filesystem::new(self.root().join("target"))
+        self.target_dir.clone().unwrap_or_else(|| {
+            Filesystem::new(self.root().join("target"))
+        })
     }
 
     /// Returns the root [replace] section of this workspace.
index 093e95944446cbca649ec05ac3867791c3d0e2b6..b120ace95e19de96985cdb283ebe6c7426c4580f 100644 (file)
@@ -89,7 +89,7 @@ pub fn install(root: Option<&str>,
         Some(Filesystem::new(config.cwd().join("target-install")))
     };
 
-    let ws = Workspace::one(pkg, config, overidden_target_dir);
+    let ws = try!(Workspace::one(pkg, config, overidden_target_dir));
     let pkg = try!(ws.current());
 
     // Preflight checks to check up front whether we'll overwrite something.
index f2a90a145a0a26a15ee76ded2f201e4f93d2bbbc..8201a5fa867368d5dd8c89bf9c6684963cdc5e2f 100644 (file)
@@ -266,7 +266,7 @@ fn run_verify(ws: &Workspace, tar: &File, opts: &PackageOpts) -> CargoResult<()>
     let new_pkg = Package::new(new_manifest, &manifest_path);
 
     // Now that we've rewritten all our path dependencies, compile it!
-    let ws = Workspace::one(new_pkg, config, None);
+    let ws = try!(Workspace::one(new_pkg, config, None));
     try!(ops::compile_ws(&ws, None, &ops::CompileOptions {
         config: config,
         jobs: opts.jobs,
index 32bbe72384eea9296f06813c2a8d9c476ae7d53d..e3a33eab0b7cb10021aba0a313c9a5694eb4be5f 100644 (file)
@@ -28,7 +28,6 @@ pub struct Config {
     values: LazyCell<HashMap<String, ConfigValue>>,
     cwd: PathBuf,
     rustdoc: LazyCell<PathBuf>,
-    target_dir: Option<Filesystem>,
     extra_verbose: Cell<bool>,
     frozen: Cell<bool>,
     locked: Cell<bool>,
@@ -37,23 +36,18 @@ pub struct Config {
 impl Config {
     pub fn new(shell: MultiShell,
                cwd: PathBuf,
-               homedir: PathBuf) -> CargoResult<Config> {
-        let mut cfg = Config {
+               homedir: PathBuf) -> Config {
+        Config {
             home_path: Filesystem::new(homedir),
             shell: RefCell::new(shell),
             rustc: LazyCell::new(),
             cwd: cwd,
             values: LazyCell::new(),
             rustdoc: LazyCell::new(),
-            target_dir: None,
             extra_verbose: Cell::new(false),
             frozen: Cell::new(false),
             locked: Cell::new(false),
-        };
-
-        try!(cfg.scrape_target_dir_config());
-
-        Ok(cfg)
+        }
     }
 
     pub fn default() -> CargoResult<Config> {
@@ -65,7 +59,7 @@ impl Config {
             human("Cargo couldn't find your home directory. \
                   This probably means that $HOME was not set.")
         }));
-        Config::new(shell, cwd, homedir)
+        Ok(Config::new(shell, cwd, homedir))
     }
 
     pub fn home(&self) -> &Filesystem { &self.home_path }
@@ -108,8 +102,15 @@ impl Config {
 
     pub fn cwd(&self) -> &Path { &self.cwd }
 
-    pub fn target_dir(&self) -> Option<&Filesystem> {
-        self.target_dir.as_ref()
+    pub fn target_dir(&self) -> CargoResult<Option<Filesystem>> {
+        if let Some(dir) = env::var_os("CARGO_TARGET_DIR") {
+            Ok(Some(Filesystem::new(self.cwd.join(dir))))
+        } else if let Some(val) = try!(self.get_path("build.target-dir")) {
+            let val = self.cwd.join(val.val);
+            Ok(Some(Filesystem::new(val)))
+        } else {
+            Ok(None)
+        }
     }
 
     fn get(&self, key: &str) -> CargoResult<Option<ConfigValue>> {
@@ -363,16 +364,6 @@ impl Config {
         }
     }
 
-    fn scrape_target_dir_config(&mut self) -> CargoResult<()> {
-        if let Some(dir) = env::var_os("CARGO_TARGET_DIR") {
-            self.target_dir = Some(Filesystem::new(self.cwd.join(dir)));
-        } else if let Some(val) = try!(self.get_path("build.target-dir")) {
-            let val = self.cwd.join(val.val);
-            self.target_dir = Some(Filesystem::new(val));
-        }
-        Ok(())
-    }
-
     fn get_tool(&self, tool: &str) -> CargoResult<PathBuf> {
         let var = tool.chars().flat_map(|c| c.to_uppercase()).collect::<String>();
         if let Some(tool_path) = env::var_os(&var) {